<?php

namespace app\index\controller;

use tian\Session;
use app\model\MarketEquip;
use app\model\MarketProp;

class Market extends Base
{
    public function index()
    {
        try {
            $this->fetch('index');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function equip()
    {
        try {
            $role_id = Session::get('uid');
            $zbtool = input('zbtool');
            $zbname = input('zbname');
            $qianghua = input('qianghua');

            $where = [];
            $pageMaps = [];

            if ($zbtool) {
                $where[] = ['zbtool', '=', $zbtool];
            }
            if ($zbname) {
                $where[] = ['zbname', 'like', '%' . $zbname . '%'];
            }
            if ($qianghua) {
                $where[] = ['qianghua', '=', $qianghua];
            }

            //URL参数
            $pageMaps['zbtool'] = $zbtool;
            $pageMaps['zbname'] = $zbname;
            $pageMaps['qianghua'] = $qianghua;

            $pageAry = [
                'list_rows' => 5,
                'page' => input('page', 1),
                'path' => 'equip',
                'query' => $pageMaps,
            ];

            $modelMarketEquip = new MarketEquip();
            $list = $modelMarketEquip->loadList($where, $pageAry);
            if(!empty($list)){
                foreach ($list as $key=>$val){
                    $val['zbtoolName'] = model('Equip')->getZbtoolAttr($val['zbtool']);
                    $val['str'] = str_base64_encode($val['payid'], $role_id);
                }
            }
            $this->assign('list', $list);
            $this->assign('_page', $list->render());
            $this->assign('pageMaps', $pageMaps);

            $zbtool_list = model('Equip')->zbtool();
            $this->assign('zbtool_list', $zbtool_list);

            //我的
            $whereMy = [];
            $whereMy[] = ['uid', '=', $role_id];
            $listMy = $modelMarketEquip->loadList($whereMy, $pageAry);
            if(!empty($listMy)){
                foreach ($listMy as $key=>$val){
                    $val['zbtoolName'] = model('Equip')->getZbtoolAttr($val['zbtool']);
                    $val['str'] = str_base64_encode($val['payid'], $role_id);
                }
            }
            $this->assign('listMy', $listMy);


            $this->fetch('equip');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function equipAct()
    {
        try {
            $role_id = Session::get('uid');
            $modelMarketEquip = new MarketEquip();
            $modelRole = model('Role');

            $act = input('act');
            $str = input('str');

            $id = $this->strAnalysis($str, $role_id);
            $infoMarketEquip = $modelMarketEquip->getInfo($id);
            if(empty($infoMarketEquip)){
                returnJson(0, '非法操作');
            }

            $map = [];
            $map['zbnowid'] = $infoMarketEquip['zbnowid'];
            $map['status'] = 1;

            if ($act == 'buy') {//购
                $map['uid'] = $role_id;

                $infoRole = $modelRole->getInfo($role_id);
                if($infoRole['uyxb'] < $infoMarketEquip['pay']){
                    returnJson(0, '灵石不足');
                }

                $tempBuyer = [
                    'uid'=>$role_id,
                    'uyxb'=>ceil($infoRole['uyxb'] - $infoMarketEquip['pay']),
                ];
                $modelRole->saved($tempBuyer);

                $infoRoleSeller = $modelRole->getInfo($infoMarketEquip['uid']);
                $tempSeller = [
                    'uid'=>$infoMarketEquip['uid'],
                    'uyxb'=>ceil($infoRoleSeller['uyxb'] + $infoMarketEquip['pay']),
                ];
                $modelRole->saved($tempSeller);
            }

            if ($act == 'back') {//下

            }

            model('RoleEquip')->saved($map);
            $modelMarketEquip->del($id);

            returnJson(1, 'ok');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function prop()
    {
        try {
            $role_id = Session::get('uid');
            $djname = input('djname');

            $where = [];
            $pageMaps = [];

            if ($djname) {
                $where[] = ['djname', 'like', '%' . $djname . '%'];
            }

            //URL参数
            $pageMaps['djname'] = $djname;

            $pageAry = [
                'list_rows' => 5,
                'page' => input('page', 1),
                'path' => 'prop',
                'query' => $pageMaps,
            ];

            $modelMarketProp = new MarketProp();
            $list = $modelMarketProp->loadList($where, $pageAry);
            if(!empty($list)){
                foreach ($list as $key=>$val){
                    $val['str'] = str_base64_encode($val['payid'], $role_id);
                }
            }
            $this->assign('list', $list);
            $this->assign('_page', $list->render());
            $this->assign('pageMaps', $pageMaps);

            //我的
            $whereMy = [];
            $whereMy[] = ['uid', '=', $role_id];
            $listMy = $modelMarketProp->loadList($whereMy, $pageAry);
            if(!empty($listMy)){
                foreach ($listMy as $key=>$val){
                    $val['str'] = str_base64_encode($val['payid'], $role_id);
                }
            }
            $this->assign('listMy', $listMy);

            $this->fetch('prop');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }

    public function propAct()
    {
        try {
            $role_id = Session::get('uid');
            $modelMarketProp = new MarketProp();
            $modelRole = model('Role');
            $modelRoleProp = model('RoleProp');

            $act = input('act');
            $str = input('str');

            $id = $this->strAnalysis($str, $role_id);
            $infoMarketProp = $modelMarketProp->getInfo($id);
            if(empty($infoMarketProp)){
                returnJson(0, '非法操作');
            }

            if ($act == 'buy') {//购
                $infoRole = $modelRole->getInfo($role_id);
                if($infoRole['uyxb'] < $infoMarketProp['pay']){
                    returnJson(0, '灵石不足');
                }

                $tempBuyer = [
                    'uid'=>$role_id,
                    'uyxb'=>ceil($infoRole['uyxb'] - $infoMarketProp['pay']),
                ];
                $modelRole->saved($tempBuyer);

                $infoRoleSeller = $modelRole->getInfo($infoMarketProp['uid']);
                $tempSeller = [
                    'uid'=>$infoMarketProp['uid'],
                    'uyxb'=>ceil($infoRoleSeller['uyxb'] + $infoMarketProp['pay']),
                ];
                $modelRole->saved($tempSeller);

                $modelRoleProp->adddj($role_id,$infoMarketProp['djid'],$infoMarketProp['djcount']);
            }

            if ($act == 'back') {//下
                $modelRoleProp->adddj($infoMarketProp['uid'],$infoMarketProp['djid'],$infoMarketProp['djcount']);
            }

            $modelMarketProp->del($id);

            returnJson(1, 'ok');
        } catch (\Throwable $e) {
            echo $e->getMessage();
        }
    }
    
}
